﻿using Product.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;

namespace Product
{
    public partial class FrmProductsControl : FrmFather
    {
        public FrmProductsControl()
        {
            InitializeComponent();
            this.LblTitle.Text = this.Text;
            this.LblTitle2.Text = LblTitle.Text;
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;
        }

        int selectedIndex = 0;
        public static int proID = 0;
        public static bool isAnalyze = false;
        private static bool isFirstTimeLoad = true;
        internal static bool isProCreate = false;
        internal static DataGridViewRow selectRow;
        UBOXDataSet ds = new UBOXDataSet();
        DataGridViewButtonColumn btn = new DataGridViewButtonColumn();


        private void FrmProductsContol_Load(object sender, EventArgs e)
        {
            // TODO:  這行程式碼會將資料載入 'uBOXDataSet.brand' 資料表。您可以視需要進行移動或移除。
            this.brandTableAdapter.Fill(this.uBOXDataSet.brand);
            // TODO:  這行程式碼會將資料載入 'uBOXDataSet.category' 資料表。您可以視需要進行移動或移除。
            this.categoryTableAdapter.Fill(this.uBOXDataSet.category);
            // TODO:  這行程式碼會將資料載入 'uBOXDataSet.production_rom' 資料表。您可以視需要進行移動或移除。
            this.production_romTableAdapter.Fill(this.uBOXDataSet.production_rom);


            if (isFirstTimeLoad)
            {
                DataGridViewButtonColumn btnModify = ColumnButton();
                this.dataGridView.Columns.Insert(0, btnModify);
                isFirstTimeLoad = false;
            }
            DataGridViewFill();
            DataGridViewFormat();
        }

        #region FillDataGridView

        private void DataGridViewFill()
        {
            ds.Clear();
         
            this.productTableAdapter1.Fill(this.ds.Product);
            this.dataGridView.DataSource = this.ds.Product;

        }
        #endregion

        #region GridViewFormat
        private void DataGridViewFormat()
        {
            if (this.dataGridView.DataSource != null)
            {
                for (int i = 21; i < this.dataGridView.Columns.Count; i++)
                {
                    this.dataGridView.Columns[i].Visible = false;
                }
                this.dataGridView.Columns["product_id"].HeaderText = "編號";
                this.dataGridView.Columns["product_name"].HeaderText = "商品";
                this.dataGridView.Columns["size"].HeaderText = "尺寸";
                this.dataGridView.Columns["weight"].HeaderText = "重量";
                this.dataGridView.Columns["battery_capacity"].HeaderText = "電池容量";
                this.dataGridView.Columns["battery_talk_time"].HeaderText = "使用時間";
                this.dataGridView.Columns["color"].HeaderText = "顏色";
                this.dataGridView.Columns["price"].HeaderText = "價格";
                this.dataGridView.Columns["price"].DefaultCellStyle.Format = "c0";
                this.dataGridView.Columns["category_name"].HeaderText = "類別";
                this.dataGridView.Columns["brand_name"].HeaderText = "品牌";
                this.dataGridView.Columns["connection_standard"].HeaderText = "通訊標準";
                this.dataGridView.Columns["operating_system"].HeaderText = "OS";
                this.dataGridView.Columns["processor_core"].HeaderText = "核心";
                this.dataGridView.Columns["processor_speed"].HeaderText = "速度";
                this.dataGridView.Columns["screen_size"].HeaderText = "螢幕尺寸";
                this.dataGridView.Columns["screen_resolution"].HeaderText = "螢幕解析度";
                this.dataGridView.Columns["ram"].HeaderText = "記憶體";
                this.dataGridView.Columns["rom"].HeaderText = "容量";
                this.dataGridView.Columns["rear"].HeaderText = "後鏡頭";
                this.dataGridView.Columns["front"].HeaderText = "前鏡頭";
                this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.dataGridView.Sort(this.dataGridView.Columns["product_id"], ListSortDirection.Ascending);
            }
        }
        #endregion

        #region ViewButton
        private DataGridViewButtonColumn ColumnButton()
        {
            DataGridViewCellStyle btnModifyStyle = new DataGridViewCellStyle();

            btnModifyStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            btnModifyStyle.ForeColor = System.Drawing.Color.Red;
            btnModifyStyle.Padding = new System.Windows.Forms.Padding(2);
            btn.DefaultCellStyle = btnModifyStyle;
            btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            btn.HeaderText = "修改資料";
            btn.Text = "檢視";
            btn.Frozen = true;
            btn.UseColumnTextForButtonValue = true;


            return btn;
        }
        #endregion


        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.AppStarting;
            this.btnAddProduct.Enabled = false;
            isProCreate = true;
            FrmProductManager f = new FrmProductManager();
            f.ShowDialog();
            btnSelect_Click(sender, e);
            this.btnAddProduct.Enabled = true;
            this.Cursor = Cursors.Default;
            FrmProductsContol_Load(sender, e);
        }

        
        void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                selectedIndex = e.RowIndex;
                proID = Convert.ToInt32(this.dataGridView.Rows[e.RowIndex].Cells["product_id"].Value);
                isProCreate = false;
                int rowIndex = this.dataGridView.SelectedCells[0].RowIndex;
                selectRow = this.dataGridView.Rows[rowIndex];
                FrmProductManager f = new FrmProductManager();
                f.ShowDialog();
                FrmProductsContol_Load(sender, e);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.btnSelect.Enabled = this.lblWarning.Visible = false;
            this.Cursor = Cursors.WaitCursor;
            if ((chkID.Checked && this.txtID.Text == "") ||
                (chkPrice.Checked && this.txtPrice1.Text == "") ||
                (chkPrice.Checked && this.txtPrice2.Text == "") ||
                (chkName.Checked && this.txtName.Text == ""))
            {
                MessageBox.Show("請輸入完整查詢資訊");
                this.Cursor = Cursors.Default;
            }
            else
            {
                this.dataGridView.Columns.Clear();
                this.dataGridView.DataSource = null;
                var q = from p in ds.Product
                        where
                        (chkID.Checked ? int.Parse(this.txtID.Text) == p.product_id : true) &&
                        (chkName.Checked ? p.product_name.ToLower().Contains(this.txtName.Text.ToLower()) : true) &&
                        (chkPrice.Checked ? p.price >= decimal.Parse(this.txtPrice1.Text) && p.price <= decimal.Parse(this.txtPrice2.Text) : true) &&
                        (chkCategory.Checked ? (int)this.cboCategory.SelectedValue == p.category_id : true) &&
                        (chkBrand.Checked ? (int)this.cboBrand.SelectedValue == p.brand_id : true) &&
                        (chkRom.Checked ? (int)this.cboRom.SelectedValue == p.rom_id : true)
                        select p;

                if (q.Any()) // Don't use Query.Count(). "Any()" is more efficient than "Count()".
                {
                    DataGridViewButtonColumn btnModify = ColumnButton();
                    this.dataGridView.Columns.Insert(0, btnModify);
                    this.dataGridView.DataSource = q.AsDataView();
                    this.dataGridView.CurrentCell = this.dataGridView.Rows[selectedIndex].Cells[1];
                }
                else
                {
                    this.lblWarning.Visible = true;
                }
                
                DataGridViewFormat();
            }
            this.Cursor = Cursors.Default;
            this.btnSelect.Enabled = true;
            btnSelect_MouseLeave(sender, e);
        }

        #region 輸入限制-數字
        private void KeyPress_TextChanged(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 | (int)e.KeyChar > 57) & (int)e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }
        #endregion

        private void btnAddAnalyze_Click(object sender, EventArgs e)
        {
            proID = this.dataGridView.CurrentRow.Index + 1;
            this.Close();
        }

        #region 按鈕樣式

        private void btnSelect_MouseEnter(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 0;
            this.btnSelect.ForeColor = Color.White;
        }

        private void btnSelect_MouseLeave(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 1;
            this.btnSelect.ForeColor = Color.Black;
        }

        #endregion


    }
}
